package com.qiangesoft.bootcodegen.controller;

import com.qiangesoft.bootcodegen.entity.BcgDataSource;
import com.qiangesoft.bootcodegen.framework.log.Log;
import com.qiangesoft.bootcodegen.pojo.bo.BcgTableBO;
import com.qiangesoft.bootcodegen.pojo.dto.BcgDataSourceDTO;
import com.qiangesoft.bootcodegen.pojo.query.BcgDataSourceQuery;
import com.qiangesoft.bootcodegen.pojo.query.PageQuery;
import com.qiangesoft.bootcodegen.pojo.vo.PageResultVO;
import com.qiangesoft.bootcodegen.service.IBcgDataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据源 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-03-13
 */
@Api(tags = "数据源")
@RequiredArgsConstructor
@RestController
@RequestMapping("/datasource")
public class BcgDataSourceController {

    private final IBcgDataSourceService dataSourceService;

    @Log(module = "数据源", operationName = "新增数据源")
    @ApiOperation(value = "新增数据源")
    @PostMapping
    public void saveDataSource(@RequestBody @Validated BcgDataSourceDTO dataSourceDTO) {
        dataSourceService.saveDataSource(dataSourceDTO);
    }

    @Log(module = "数据源", operationName = "删除数据源")
    @ApiOperation(value = "删除数据源")
    @DeleteMapping("/{id}")
    public void removeDataSource(@PathVariable List<Long> id) {
        dataSourceService.removeDataSource(id);
    }

    @Log(module = "数据源", operationName = "修改数据源")
    @ApiOperation(value = "修改数据源")
    @PutMapping("/{id}")
    public void updateDataSource(@PathVariable Long id, @RequestBody @Validated BcgDataSourceDTO dataSourceDTO) {
        dataSourceService.updateDataSource(id, dataSourceDTO);
    }

    @Log(module = "数据源", operationName = "查询数据源")
    @ApiOperation(value = "查询数据源")
    @GetMapping
    public PageResultVO<BcgDataSource> listDataSource(@Validated PageQuery pageQuery, BcgDataSourceQuery dataSourceQuery) {
        return dataSourceService.listDataSource(pageQuery, dataSourceQuery);
    }

    @Log(module = "数据源", operationName = "数据源列表")
    @ApiOperation(value = "数据源列表")
    @GetMapping("/list")
    public List<BcgDataSource> listAllDataSource(BcgDataSourceQuery dataSourceQuery) {
        return dataSourceService.listAllDataSource(dataSourceQuery);
    }

    @Log(module = "数据源", operationName = "查询数据库表")
    @ApiOperation(value = "查询数据库表")
    @GetMapping("/table")
    public PageResultVO<BcgTableBO> listTable(@Validated PageQuery pageQuery, Long dataSourceId) {
        return dataSourceService.listTable(pageQuery, dataSourceId);
    }

    @Log(module = "数据源", operationName = "连接数据源")
    @ApiOperation(value = "连接数据源")
    @PostMapping("/connection/{id}")
    public void connection(@PathVariable Long id) {
        dataSourceService.connection(id);
    }

}
